using System;
using System.Collections.Generic;
using WegasManagementSystem.DAL;
using WegasManagementSystem.Models;

namespace WegasManagementSystem.BLL
{
    /// <summary>
    /// 用户业务逻辑类
    /// </summary>
    public class UserBLL
    {
        private UserDAL userDAL;

        /// <summary>
        /// 构造函数
        /// </summary>
        public UserBLL()
        {
            userDAL = new UserDAL();
        }

        /// <summary>
        /// 验证用户登录
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <returns>登录成功返回用户对象，失败返回null</returns>
        public User ValidateUser(string username, string password)
        {
            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
                return null;

            return userDAL.ValidateUser(username, password);
        }

        /// <summary>
        /// 添加新用户
        /// </summary>
        /// <param name="user">用户对象</param>
        /// <returns>新用户ID，失败返回0</returns>
        public int AddUser(User user)
        {
            if (user == null)
                throw new ArgumentNullException("user", "用户对象不能为空");

            if (string.IsNullOrEmpty(user.Username))
                throw new ArgumentException("用户名不能为空");

            if (string.IsNullOrEmpty(user.Password))
                throw new ArgumentException("密码不能为空");

            if (string.IsNullOrEmpty(user.UserRole))
                throw new ArgumentException("用户角色不能为空");

            // 检查用户名是否已存在
            if (userDAL.CheckUsernameExists(user.Username))
                throw new Exception("用户名已存在");

            return userDAL.AddUser(user);
        }

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="user">用户对象</param>
        /// <returns>更新成功返回true，失败返回false</returns>
        public bool UpdateUser(User user)
        {
            if (user == null)
                throw new ArgumentNullException("user", "用户对象不能为空");

            if (string.IsNullOrEmpty(user.Username))
                throw new ArgumentException("用户名不能为空");

            if (string.IsNullOrEmpty(user.UserRole))
                throw new ArgumentException("用户角色不能为空");

            // 检查用户名是否已存在（排除当前用户）
            if (userDAL.CheckUsernameExists(user.Username) && GetUserById(user.UserId)?.Username != user.Username)
                throw new Exception("用户名已存在");

            return userDAL.UpdateUser(user);
        }

        /// <summary>
        /// 修改用户密码
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="newPassword">新密码</param>
        /// <returns>修改成功返回true，失败返回false</returns>
        public bool ChangePassword(int userId, string newPassword)
        {
            if (userId <= 0)
                throw new ArgumentException("无效的用户ID");

            if (string.IsNullOrEmpty(newPassword))
                throw new ArgumentException("新密码不能为空");

            return userDAL.ChangePassword(userId, newPassword);
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>删除成功返回true，失败返回false</returns>
        public bool DeleteUser(int userId)
        {
            if (userId <= 0)
                throw new ArgumentException("无效的用户ID");

            return userDAL.DeleteUser(userId);
        }

        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <returns>用户列表</returns>
        public List<User> GetAllUsers()
        {
            return userDAL.GetAllUsers();
        }

        /// <summary>
        /// 根据ID获取用户
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>用户对象</returns>
        public User GetUserById(int userId)
        {
            if (userId <= 0)
                throw new ArgumentException("无效的用户ID");

            return userDAL.GetUserById(userId);
        }
    }
}